package com.fastloan.mgt.invest.controller.chat;

import com.fastloan.mgt.invest.model.InvestSubject;
import com.fastloan.mgt.invest.model.InvestTemp;
import com.fastloan.mgt.invest.model.vo.InvestSubjectChatVo;
import com.fastloan.mgt.invest.service.InvestSubjectService;
import com.fastloan.mgt.invest.service.InvestTempService;
import com.fastloan.mgt.invest.service.InvestTempVarService;
import com.fastloan.safe.core.util.ManagerKit;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 微信专用接口
 *
 * @author rankai
 *         createTime 2017-08-2017/8/9 10:09
 */
@RestController
@RequestMapping("/api/chat/anon")
public class ChatController extends BaseController<InvestSubject> {

    @Autowired
    private InvestTempService investTempService;

    @Autowired
    private InvestTempVarService investTempVarService;

    @Autowired
    private InvestSubjectService investSubjectService;


    /**
     * 根据客户经理code获得模版列表
     *
     * @param code 客户经理code码
     * @return
     */
    @GetMapping("/weiXin/selectTempByCode")
    public ResultTo selectTempByManagerCode(@RequestParam("code") Integer code) {
        List<InvestTemp> investTemps = investTempService.selectTempByManagerCode(code);
        if (investTemps == null) {
            return new ResultTo(5020, "没有对应数据");
        }
        return new ResultTo().setData(investTemps);
    }

    /**
     * 根据客户经理Id获得模版列表
     */
    @GetMapping("/weiXin/selectTempById")
    public ResultTo selectTempByManagerId() {
        List<InvestSubjectChatVo> investTemps = investTempService.selectSubjectByManagerId();
        if (investTemps == null) {
            return new ResultTo(5020, "没有对应数据");
        }
        return new ResultTo().setData(investTemps);
    }

    /**
     * 生成二维码
     *
     * @param tempId 模版ID
     * @return
     */
    @GetMapping("/weiXin/{tempId}/{subjectId}")
    public ResultTo createUrl(@PathVariable("tempId") String tempId,
                              @PathVariable("subjectId") String subjectId) {
        return new ResultTo().setData("/#/phone/info/" + tempId + "/" + ManagerKit.getUserId() + "/" + subjectId);
    }

    /**
     * 根据客户经理Id获取项目数据
     *
     * @return
     */
    @GetMapping("/weiXin/selectSubjectById")
    public ResultTo selectSubjectByManagerId() {
        List<InvestSubject> investSubjects = investSubjectService.selectSubjectByManagerId();
        if (investSubjects == null) {
            return new ResultTo(5020, "没有对应数据");
        }
        return new ResultTo().setData(investSubjects);
    }

    /**
     * 根据二维码获得表单信息
     *
     * @param tempId    模版ID
     * @param managerId 客户经理Id
     * @param subjectId 项目ID
     * @param typeId    模版类型Id
     * @return
     */
    @GetMapping("/safe/{tempId}/{managerId}/{subjectId}")
    public ResultTo selectTempByUrl(@PathVariable("tempId") String tempId,
                                    @PathVariable("managerId") String managerId,
                                    @PathVariable("subjectId") String subjectId,
                                    @RequestParam(value = "typeId", required = false) String typeId) {
        InvestSubject investSubject = investSubjectService.selectByPrimaryKey(subjectId);
        if (investSubject == null || investSubject.getState() == 0 || investSubject.getState() == 2) {
            return new ResultTo(ResultEnum.OPERATION_FAILED, "该项目不存在或已经停止");
        }
        Map<String, Object> map = investTempVarService.selectTempByUrl(tempId, managerId, typeId);
        return new ResultTo().setData(map);
    }

    /**
     * 根据模版Id获取表单数据
     *
     * @param tempId 模版Id
     * @return
     */
    @GetMapping("/safe/tempVar")
    public ResultTo selectTempVar(@RequestParam("tempId") String tempId,
                                  @RequestParam("typeId") String typeId) {
        return new ResultTo().setData(investTempVarService.selectByTempId(tempId, typeId));
    }
}
